package website.haicheng.cloudauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.haicheng.cloudauth.dto.MenuDto;
import website.haicheng.cloudauth.entity.Menu;
import website.haicheng.cloudauth.mapper.MenuMapper;
import website.haicheng.cloudauth.service.IMenuService;
import website.haicheng.cloudauth.vo.MenuVo;
import website.haicheng.core.constant.SysConstant;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

import static website.haicheng.core.util.Assert.stat;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 582895699@qq.com
 * @since 2021-02-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Set<String> listPermission(int userId) {
        if (SysConstant.ROOT_ID == userId) {
            Set<String> strings = Sets.newHashSetWithExpectedSize(1);
            strings.add("*");
            return strings;
        }
        return menuMapper.listPermissionByUserId(userId);
    }

    @Override
    public void addMenu(MenuDto dto, String createBy) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(dto, menu);
        menu.setCreateBy(createBy);
        menu.setCreateTime(LocalDateTime.now());
        menuMapper.insert(menu);
    }

    @Override
    public void updateMenu(MenuDto dto, String modifyBy) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(dto, menu);
        menu.setModifyBy(modifyBy);
        menu.setUpdateTime(LocalDateTime.now());
        menuMapper.updateById(menu);
    }

    @Override
    public void delMenu(long id, String modifyBy) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getParentId, id);
        queryWrapper.lambda().eq(Menu::getIsDelete, false);
        Integer count = menuMapper.selectCount(queryWrapper);
        stat(count > 0, "请先删除子菜单");

        Menu menu = new Menu();
        menu.setId(id);
        menu.setUpdateTime(LocalDateTime.now());
        menu.setModifyBy(modifyBy);
        menuMapper.updateById(menu);
    }

    @Override
    public List<MenuVo> listMenus() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getIsDelete, false);
        queryWrapper.orderByAsc("id", "sort");
        List<Menu> list = menuMapper.selectList(queryWrapper);

        List<MenuVo> menuVoList = Lists.newArrayListWithExpectedSize(list.size());
        list.forEach(item -> {
            MenuVo vo = new MenuVo();
            BeanUtils.copyProperties(item, vo);
            menuVoList.add(vo);
        });
        return menuVoList;
    }
}
